package com.zy.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.entity.UserWorkStatus;
import com.zy.entity.WorkTime;
import com.zy.mapper.UserWorkStatusManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

@Controller
public class WorkController {
    @Autowired
    private UserWorkStatusManageMapper userWorkStatusManageMapper;

    /**
     * 用户考勤信息
     *
     * @return
     */
    @RequestMapping("/listUserWorkStatus")
    public ModelAndView listUserWorkStatus(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                           @RequestParam(value = "queryName", required = false) String queryName) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("UserWorkStatusManage");

        //考勤分页
        IPage<UserWorkStatus> statusPage = new Page<>(pageNo, pageSize);

        HashMap map = new HashMap();
        if (!StringUtils.isEmpty(queryName)) {
            map.put("queryName", queryName);
        } else {
            queryName = "";
        }
        IPage<UserWorkStatus> userWorkStatus = userWorkStatusManageMapper.listUserWorkStatus(statusPage, map);

        //考勤时间
        WorkTime workTime = userWorkStatusManageMapper.getWorkTime();
        modelAndView.addObject("queryName", queryName);
        modelAndView.addObject("userWorkStatus", userWorkStatus);
        modelAndView.addObject("workTime", workTime);
        return modelAndView;
    }

    /**
     * 删除用户考勤信息
     */
    @RequestMapping("/deleteUserWorkStatus")
    public String deleteUserWorkStatus(Integer statusId, Integer pageNo) {
        userWorkStatusManageMapper.deleteUserWorkStatus(statusId);
        return "";
    }

    /**
     * 修改考勤时间
     */
    @RequestMapping("/modifyWorkTime")
    public String modifyWorkTime(String ot01, String ot02, String ot03,
                                 String ft01, String ft02, String ft03) {

        String onWork = ot01 + ":" + ot02 + ":" + ot03;
        String offWork = ft01 + ":" + ft02 + ":" + ft03;
        userWorkStatusManageMapper.modifyWorkTime(onWork, offWork);
        return "redirect:/listUserWorkStatus";
    }

}
